/**
 * JavaScript CSV处理示例
 * 演示Papa Parse的各种用法
 */

const fs = require('fs');
const Papa = require('papaparse');

/**
 * 示例1: 基本信息（只读取前100行）
 */
function example1_basicInfo(filePath) {
    return new Promise((resolve, reject) => {
        console.log('\n' + '='.repeat(50));
        console.log('示例1: 获取CSV基本信息');
        console.log('='.repeat(50));

        let rowCount = 0;
        const maxRows = 100;
        const rows = [];
        let headers = [];

        const stream = fs.createReadStream(filePath, { encoding: 'utf-8' });

        Papa.parse(stream, {
            header: true,
            step: function(result, parser) {
                if (rowCount === 0) {
                    headers = result.meta.fields;
                }

                if (rowCount < maxRows) {
                    rows.push(result.data);
                    rowCount++;
                } else {
                    parser.abort();
                }
            },
            complete: function() {
                console.log(`\n列名 (${headers.length} 列):`);
                headers.forEach((col, idx) => {
                    console.log(`  ${idx + 1}. ${col}`);
                });

                console.log(`\n前5行数据:`);
                console.table(rows.slice(0, 5));

                resolve({ headers, rows });
            },
            error: reject
        });
    });
}

/**
 * 示例2: 统计总行数
 */
function example2_countRows(filePath) {
    return new Promise((resolve, reject) => {
        console.log('\n' + '='.repeat(50));
        console.log('示例2: 统计总行数');
        console.log('='.repeat(50));

        let rowCount = 0;

        const stream = fs.createReadStream(filePath, { encoding: 'utf-8' });

        Papa.parse(stream, {
            header: true,
            step: function() {
                rowCount++;
                if (rowCount % 50000 === 0) {
                    process.stdout.write(`\r已统计: ${rowCount.toLocaleString()} 行`);
                }
            },
            complete: function() {
                console.log(`\n\n总行数: ${rowCount.toLocaleString()}`);
                resolve(rowCount);
            },
            error: reject
        });
    });
}

/**
 * 示例3: 过滤数据并导出
 */
function example3_filterData(inputPath, outputPath, filterFunc = null) {
    return new Promise((resolve, reject) => {
        console.log('\n' + '='.repeat(50));
        console.log('示例3: 过滤数据');
        console.log('='.repeat(50));

        let totalRows = 0;
        let filteredRows = 0;
        const batchSize = 10000;
        const batch = [];
        let isFirstWrite = true;

        const stream = fs.createReadStream(inputPath, { encoding: 'utf-8' });

        Papa.parse(stream, {
            header: true,
            step: function(result) {
                totalRows++;

                // 应用过滤条件
                const shouldInclude = filterFunc ? filterFunc(result.data) : true;

                if (shouldInclude) {
                    batch.push(result.data);
                    filteredRows++;

                    // 批量写入
                    if (batch.length >= batchSize) {
                        writeBatch(outputPath, batch, isFirstWrite);
                        isFirstWrite = false;
                        batch.length = 0;
                    }
                }

                if (totalRows % 50000 === 0) {
                    process.stdout.write(`\r已处理: ${totalRows.toLocaleString()} 行, 保留: ${filteredRows.toLocaleString()} 行`);
                }
            },
            complete: function() {
                // 写入剩余数据
                if (batch.length > 0) {
                    writeBatch(outputPath, batch, isFirstWrite);
                }

                console.log(`\n\n完成! 共保留 ${filteredRows.toLocaleString()} 行`);
                console.log(`输出文件: ${outputPath}`);
                resolve({ totalRows, filteredRows });
            },
            error: reject
        });
    });
}

/**
 * 辅助函数: 批量写入
 */
function writeBatch(filePath, rows, isFirst) {
    const csv = Papa.unparse(rows, { header: isFirst });
    fs.appendFileSync(filePath, csv + '\n', 'utf-8');
}

/**
 * 示例4: 列统计信息
 */
function example4_columnStatistics(filePath) {
    return new Promise((resolve, reject) => {
        console.log('\n' + '='.repeat(50));
        console.log('示例4: 列统计信息');
        console.log('='.repeat(50));

        let headers = [];
        const stats = {};
        let totalRows = 0;

        const stream = fs.createReadStream(filePath, { encoding: 'utf-8' });

        Papa.parse(stream, {
            header: true,
            step: function(result) {
                totalRows++;

                if (totalRows === 1) {
                    headers = result.meta.fields;
                    headers.forEach(col => {
                        stats[col] = {
                            count: 0,
                            missing: 0,
                            unique: new Set()
                        };
                    });
                }

                // 统计每列
                headers.forEach(col => {
                    const value = result.data[col];
                    stats[col].count++;

                    if (value === null || value === undefined || value === '') {
                        stats[col].missing++;
                    } else {
                        // 只保留前10000个唯一值（避免内存溢出）
                        if (stats[col].unique.size < 10000) {
                            stats[col].unique.add(value);
                        }
                    }
                });

                if (totalRows % 50000 === 0) {
                    process.stdout.write(`\r已统计: ${totalRows.toLocaleString()} 行`);
                }
            },
            complete: function() {
                console.log('\n\n列统计信息:');
                headers.forEach((col, idx) => {
                    const stat = stats[col];
                    const missingPct = ((stat.missing / stat.count) * 100).toFixed(2);
                    const uniqueCount = stat.unique.size;

                    console.log(`\n${idx + 1}. ${col}:`);
                    console.log(`   总计: ${stat.count.toLocaleString()}`);
                    console.log(`   缺失: ${stat.missing.toLocaleString()} (${missingPct}%)`);
                    console.log(`   唯一值: ${uniqueCount >= 10000 ? '10000+' : uniqueCount.toLocaleString()}`);
                });

                resolve(stats);
            },
            error: reject
        });
    });
}

/**
 * 示例5: 查找特定条件的行
 */
function example5_searchRows(filePath, searchFunc) {
    return new Promise((resolve, reject) => {
        console.log('\n' + '='.repeat(50));
        console.log('示例5: 搜索数据');
        console.log('='.repeat(50));

        let totalRows = 0;
        const matchedRows = [];
        const maxResults = 100;

        const stream = fs.createReadStream(filePath, { encoding: 'utf-8' });

        Papa.parse(stream, {
            header: true,
            step: function(result, parser) {
                totalRows++;

                if (searchFunc(result.data)) {
                    matchedRows.push(result.data);

                    if (matchedRows.length >= maxResults) {
                        parser.abort();
                    }
                }

                if (totalRows % 50000 === 0) {
                    process.stdout.write(`\r已搜索: ${totalRows.toLocaleString()} 行, 找到: ${matchedRows.length}`);
                }
            },
            complete: function() {
                console.log(`\n\n搜索完成!`);
                console.log(`已搜索: ${totalRows.toLocaleString()} 行`);
                console.log(`找到: ${matchedRows.length} 条匹配记录`);

                if (matchedRows.length > 0) {
                    console.log('\n匹配的数据 (前10条):');
                    console.table(matchedRows.slice(0, 10));
                }

                resolve(matchedRows);
            },
            error: reject
        });
    });
}

/**
 * 示例6: 数据转换
 */
function example6_transformData(inputPath, outputPath, transformFunc) {
    return new Promise((resolve, reject) => {
        console.log('\n' + '='.repeat(50));
        console.log('示例6: 数据转换');
        console.log('='.repeat(50));

        let totalRows = 0;
        const batchSize = 10000;
        const batch = [];
        let isFirstWrite = true;

        const stream = fs.createReadStream(inputPath, { encoding: 'utf-8' });

        Papa.parse(stream, {
            header: true,
            step: function(result) {
                totalRows++;

                // 应用转换函数
                const transformed = transformFunc(result.data);

                if (transformed) {
                    batch.push(transformed);

                    if (batch.length >= batchSize) {
                        writeBatch(outputPath, batch, isFirstWrite);
                        isFirstWrite = false;
                        batch.length = 0;
                    }
                }

                if (totalRows % 50000 === 0) {
                    process.stdout.write(`\r已转换: ${totalRows.toLocaleString()} 行`);
                }
            },
            complete: function() {
                if (batch.length > 0) {
                    writeBatch(outputPath, batch, isFirstWrite);
                }

                console.log(`\n\n转换完成! 共处理 ${totalRows.toLocaleString()} 行`);
                console.log(`输出文件: ${outputPath}`);
                resolve(totalRows);
            },
            error: reject
        });
    });
}

/**
 * 主函数
 */
async function main() {
    const csvFiles = [
        'output/基本设施.csv',
        'output/设施集合.csv'
    ];

    console.log('='.repeat(50));
    console.log('JavaScript CSV处理示例');
    console.log('='.repeat(50));

    console.log('\n可用的CSV文件:');
    csvFiles.forEach((file, idx) => {
        try {
            const stats = fs.statSync(file);
            const sizeMB = (stats.size / (1024 * 1024)).toFixed(2);
            console.log(`${idx + 1}. ${file} (${sizeMB} MB)`);
        } catch (e) {
            console.log(`${idx + 1}. ${file} (文件不存在)`);
        }
    });

    console.log('\n可用示例:');
    console.log('1. 获取基本信息');
    console.log('2. 统计总行数');
    console.log('3. 过滤数据 (示例: 删除空行)');
    console.log('4. 列统计信息');
    console.log('5. 运行所有示例');

    // 这里可以根据需要选择运行哪个示例
    // 默认运行示例1和示例2
    console.log('\n开始运行示例1和示例2...\n');

    try {
        const filePath = csvFiles[0];

        // 示例1: 基本信息
        await example1_basicInfo(filePath);

        // 示例2: 统计行数
        await example2_countRows(filePath);

        console.log('\n' + '='.repeat(50));
        console.log('示例运行完成!');
        console.log('='.repeat(50));
        console.log('\n提示: 修改 examples.js 来运行其他示例或自定义处理逻辑');

    } catch (error) {
        console.error('错误:', error);
    }
}

// 如果直接运行此脚本
if (require.main === module) {
    main();
}

module.exports = {
    example1_basicInfo,
    example2_countRows,
    example3_filterData,
    example4_columnStatistics,
    example5_searchRows,
    example6_transformData
};

